1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
print("print practice-------------") print("hello world")
print("I\'m ok") print("-------------") print(True and True) print(True or False) print(not False) print("-------------")
print(ord('A'))
print("-------------") print('\u4e2d\u6587') print("ABC".encode("ascii")) print("ABC".encode("utf-8")) print("中文".encode('gb2312')) print("-------------") print("1.23e9: ",1.23e9) print("name:%s,age:%d" % ("yifei",20))
print("list practice-------------") print("list,列表,是一种有序的数据集合,可以随时添加删除其中的元素") print("需要用[]包裹") classmates = ["aaa","bbb","ccc"] print("classmates:",classmates) print("len:",len(classmates)) print("取倒数第n个,",classmates[-1],classmates[-2],classmates[-3]) classmates.append("ddd") print("append element:",classmates) classmates.insert(1,"jack") print("insert element:",classmates) classmates.pop() print("delete last element:",classmates) classmates.pop(3) print("delete n\'th ele:",classmates) classmates.append(["eee","fff"]) print("append a list:",classmates)
print("tuple practice-------------") print("tuple,元组,也是有序列表,跟list非常像,但是一旦初始化就不能修改") print("用()包裹") t1=(1,2) t2=() print("t:",t1," t2:",t2) t3=(3,) print("定义只有一个元素的tuple,需要(3,) :",t3) print("如果tuple里面包含list,那么其中的list是可以变的") t=('a','b',['A','B']) print("t:",t) t[2][0]='X' t[2][1]='Y' print("t:",t)
print("if-------------") age=20 if age>=18: print("adult") elif age>=6: print("tennager") else: print("kid")
print("input-------------") birth="2009"
if birth > "2000": print("00后") else: print("00前") birth2=int(birth) print(birth2)
print("cal list sum-------------") sum=0 for x in [1,2,3,4,5,6,7,8,9,10]: sum=sum+x print(sum) print("list(range(5)):",list(range(5))) sum=0 for x in range(101): sum=sum+x print("0-100 sum:",sum)
print("while-------------") sum=0 n=99 while n>0: sum=sum+n n=n-2 print(sum)
print("依次打印list-------------") l=["tom","lisa","adam"] for x in l: print("hello",x)
print("continue and break-------------") sum2=0 nn=0 while True: if nn==5: nn=nn+1 continue if nn>=10: break nn=nn+1 sum2=sum2+nn print(sum2)
print("dict-------------") print("dict,字典,类似c++中的map,用{}包裹") d={"daming":95,"bob":99} print(d) print("d[\"daming\"]:",d["daming"]) d["sam"]=100 print(d) print("判断jack是否在dict中 \"jack\" in d:","jack" in d)
d.get("daming") d.get("ddd") print(d.get("aaa",-1)) d.pop("daming") print(d)
print("set-------------") s=set([1,1,2,2,3]) print(s)
s.add(4) print(s)
s.remove(1) print(s)
s2=set([3,4,5]) print(s & s2) print(s | s2)
print("不可变对象-------------") print("对不可变对象a调用replace,a不变") a="abc" b=a.replace('a','A') print("a: ",a) print("a:",a," b:",b)
|